package com.ktsmartcar.service;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class SocketService {

	private String serverIp;
	private int serverPort;
	private Socket socket;
    private BufferedInputStream bis;
    private BufferedOutputStream bos;
    
    
    public String getServerIp() {
		return serverIp;
	}
	public void setServerIp(String serverIp) {
		this.serverIp = serverIp;
	}
	public int getServerPort() {
		return serverPort;
	}
	public void setServerPort(int serverPort) {
		this.serverPort = serverPort;
	}
	
	/** Creates a new instance of Worker */
    public SocketService() {
        socket = null;
        bis = null;
        bos = null; 
    }
    public void connect() throws UnknownHostException, IOException {
    	socket = new Socket(InetAddress.getByName(this.serverIp), this.serverPort);
        bis = new BufferedInputStream(socket.getInputStream());
        bos = new BufferedOutputStream(socket.getOutputStream());
    }

    public void connect(String host, int port, int localPort) throws UnknownHostException, IOException {
        socket = new Socket(InetAddress.getByName(host), port, InetAddress.getLocalHost(), localPort);
    }

    public void sendMessage(String message, String userNo, String carNo) throws IOException {
    	
    	byte[] msg = hexStringToByteArray(message);
    	
    	bos.write(msg);
    	
    	for (int i = 0; i < userNo.length(); i++) {
			bos.write((int)userNo.charAt(i));
    	}
    	bos.write(carNo.getBytes("utf-8"));
    	bos.flush();    	
    }

    public String receiveMessage() throws IOException {
        
    	byte[] buf = new byte[2];
        
        int read = 0;
        String str = "";
        
        while((read = bis.read(buf)) != -1) {
        	str += byteArrayToHex(buf);
        }
        
        return  str;
    }

    public void disconnect() throws IOException {
        bis.close();
        bos.close();
        socket.close();
    }

    public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                                 + Character.digit(s.charAt(i+1), 16));
        }
        return data;
    }
    public static byte[] hexToByteArray(String hex) {
        if (hex == null || hex.length() == 0) {
            return null;
        }

        byte[] ba = new byte[hex.length() / 2];
        for (int i = 0; i < ba.length; i++) {
            ba[i] = (byte) Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16);
        }
        return ba;
    }


    public static String byteArrayToHex(byte[] ba) {
        if (ba == null || ba.length == 0) {
            return null;
        }

        StringBuffer sb = new StringBuffer(ba.length * 2);
        String hexNumber;
        for (int x = 0; x < ba.length; x++) {
            hexNumber = "0" + Integer.toHexString(0xff & ba[x]);

            sb.append(hexNumber.substring(hexNumber.length() - 2));
        }
        return sb.toString();
    }
    
    public static String HexBytesToStringForLog(byte[] btTmp){
		String strTmp = "";

		for(int i = 0; i < btTmp.length; i++){
			strTmp += String.format("%02x", btTmp[i]) + " ";
		}
		
		return strTmp;
	}
	
	public static String BinaryToString(byte[] btTmp){
		String strTmp = "";
		
		for(int i =0; i<btTmp.length; i++) strTmp += (char)btTmp[i];
		
		return strTmp;
	}
    public static byte[] intToByteArray(int value) {
        byte[] b = new byte[4];
        for (int i = 0; i < 4; i++) {
            int offset = (b.length - 1 - i) * 8;
            b[i] = (byte) ((value >>> offset) & 0xFF);
        }
        return b;
    }
}
